import Cookies from "js-cookie"
export const accessTokenKey = window.configObj.accessTokenKey || process.env.VUE_APP_ACCESSTOKEN_KEY || "vue2-admin-access-token"
export const refreshTokenKey = window.configObj.refreshTokenKey || process.env.VUE_APP_REFRESHTOKEN_KEY || "vue2-admin-refresh-token"

export function getCookie(key) {
  const keepLogin = localStorage.getItem("keepLogin")
  // token还是存储在cookie
  return (keepLogin === "1" || key === accessTokenKey) ? Cookies.get(key) : sessionStorage.getItem(key)
}

/**
 * @description: 设置cookie
 * @param {*} key
 * @param {*} value
 * @param {*} expires  单位秒
 * @return {*}
 * @author: syx
 */
export function setCookie(key, value, expires) {
  const keepLogin = localStorage.getItem("keepLogin")
  // token还是存储在cookie
  if (keepLogin === "1" || key === accessTokenKey) {
    if (expires) {
      return Cookies.set(key, value, {
        expires: new Date(new Date().getTime() + expires * 1000)
      })
    }
    return Cookies.set(key, value)
  }
  return sessionStorage.setItem(key, value)
}

export function removeCookie(key) {
  const keepLogin = localStorage.getItem("keepLogin")
  // token还是存储在cookie
  return (keepLogin === "1" || key === accessTokenKey) ? Cookies.remove(key) : sessionStorage.removeItem(key)
}
